/**
 * 
 */
package com.teamall.web.family.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.teamall.core.common.Page;
import com.teamall.core.convert.IArgConversionService;
import com.teamall.core.idproduce.ISequenceGenerator;
import com.teamall.framework.exception.BaseAppException;
import com.teamall.framework.log.LogManager;
import com.teamall.framework.util.Utils;
import com.teamall.web.family.db.arg.ProjectFamilyRelatedArg;
import com.teamall.web.family.db.arg.ProjectFamilyRelatedArg.ProjectFamilyRelatedCriteria;
import com.teamall.web.family.db.dao.ProjectFamilyRelatedDao;
import com.teamall.web.family.db.po.ProjectFamilyRelatedPO;
import com.teamall.web.family.service.IProjectFamilyRelatedService;

/**
 * <Description> <br>
 * 
 * @author codeCreater<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2014年11月11日 <br>
 * @since V1.0<br>
 * @see com.teamall.web.family.service.impl <br>
 */

@Service("projectFamilyRelatedService")
public class ProjectFamilyRelatedServiceImpl implements
        IProjectFamilyRelatedService {

    private static final LogManager logger = LogManager
            .getLogger(ProjectFamilyRelatedServiceImpl.class);

    @Autowired
    private ProjectFamilyRelatedDao projectFamilyRelatedDao;

    /**
     * 查询条件转换成Arg类的服务接口
     */
    @Resource(name = "defaultArgConversionService")
    private IArgConversionService argConversionService;

    /**
     * 主键生成器
     */
    @Resource(name = "sequenceProcGenerator")
    private ISequenceGenerator sequenceGenerator;

    @Override
    public ProjectFamilyRelatedPO selectByPrimaryKey(Integer key)
            throws BaseAppException {
        // ///////
        // TODO 根据业务场景，设置查询条件、数据校验等

        // ///////
        return projectFamilyRelatedDao.selectByPrimaryKey(key);
    }

    @Override
    public List<ProjectFamilyRelatedPO> selectByArg(
            ProjectFamilyRelatedPO record) throws BaseAppException {
        logger.debug("selectByArg begin...record={0}", record);

        // 第一种方式：自己创建arg，自行设置查询条件及操作符
        // ProjectFamilyRelatedArg arg = new ProjectFamilyRelatedArg();
        // ProjectFamilyRelatedCriteria criteria = arg.createCriteria();

        // 第二种方式：利用arg转换服务，转换出arg，带上查询条件及操作符，
        // 转换后，还可以自行对arg进行设置修改
        ProjectFamilyRelatedArg arg = argConversionService.invokeArg(
                ProjectFamilyRelatedArg.class, record);

        // ///////
        // TODO 根据业务场景，设置查询条件，示例
        // if (Utils.notEmpty(record.getUserName())) {
        // criteria.andUserNameLike(record.getUserName());
        // }
        // ///////

        return projectFamilyRelatedDao.selectByArg(arg);
    }

    @Override
    public Page<ProjectFamilyRelatedPO> selectByArgAndPage(
            ProjectFamilyRelatedPO record,
            Page<ProjectFamilyRelatedPO> resultPage) throws BaseAppException {
        logger.debug("selectByArgAndPage begin...record={0}", record);

        // 第一种方式：自己创建arg，自行设置查询条件及操作符
        // ProjectFamilyRelatedArg arg = new ProjectFamilyRelatedArg();
        // //TODO 根据业务场景，设置查询条件，示例
        // ProjectFamilyRelatedCriteria criteria = arg.createCriteria();
        // if (Utils.notEmpty(record.getUserName())) {
        // criteria.andUserNameLike(record.getUserName());
        // }

        // 第二种方式：利用arg转换服务，转换出arg，带上查询条件及操作符，
        // 转换后，还可以自行对arg进行设置修改
        ProjectFamilyRelatedArg arg = argConversionService.invokeArg(
                ProjectFamilyRelatedArg.class, record);

        resultPage = projectFamilyRelatedDao
                .selectByArgAndPage(arg, resultPage);

        return resultPage;
    }

    @Override
    public int add(ProjectFamilyRelatedPO record) throws BaseAppException {
        logger.debug("add begin...record={0}", record);

        // ///////
        // TODO 根据业务场景，进行重名校验、设置主键、设置属性默认值等
        // 获取主键
        // int pkId = sequenceGenerator.sequenceIntValue("表名","主键名");
        // record.setUserId(pkId);
        // record.setCreatedDate(new Date());
        // ///////

        return projectFamilyRelatedDao.insertSelective(record);
    }

    @Override
    public int update(ProjectFamilyRelatedPO record) throws BaseAppException {
        logger.debug("update begin...record={0}", record);

        // ///////
        // TODO 根据业务场景，进行重名校验、数据有效性校验、设置属性默认值等

        // ///////

        return projectFamilyRelatedDao.updateByPrimaryKeySelective(record);
    }

    @Override
    public int delete(ProjectFamilyRelatedPO record) throws BaseAppException {
        logger.debug("delete begin...record={0}", record);

        // ///////
        // TODO 根据业务场景，进行关联性校验、关联删除等

        // ///////

        return projectFamilyRelatedDao
                .deleteByPrimaryKey(record.getRelatedId());
    }

    /*
     * (non-Javadoc)
     * @see com.teamall.web.family.service.IProjectFamilyRelatedService#deleteByArg(com.teamall.web.family.db.po.ProjectFamilyRelatedPO)
     */
    @Override
    public int deleteByArg(ProjectFamilyRelatedPO record)
            throws BaseAppException {
        ProjectFamilyRelatedArg arg = new ProjectFamilyRelatedArg();
        // TODO 根据业务场景，设置查询条件，示例
        ProjectFamilyRelatedCriteria criteria = arg.createCriteria();
        if (Utils.notEmpty(record.getProjectId())) {
            criteria.andProjectIdEqualTo(record.getProjectId());
        }
        if (Utils.notEmpty(record.getFamilyId())) {
            criteria.andFamilyIdEqualTo(record.getFamilyId());
        }
        return projectFamilyRelatedDao.deleteByArg(arg);
    }

    /*
     * (non-Javadoc)
     * @see com.teamall.web.family.service.IProjectFamilyRelatedService#addBatch(java.util.List)
     */
    @Override
    public int addBatch(List<ProjectFamilyRelatedPO> records)
            throws BaseAppException {
        return projectFamilyRelatedDao.insertBatch(records);
    }

    /*
     * (non-Javadoc)
     * @see com.teamall.web.family.service.IProjectFamilyRelatedService#deleteByBatch(Integer, Integer[])
     */
    @Override
    public int deleteByBatch(Integer project, Integer[] familyIds)
            throws BaseAppException {
        ArrayList<Integer> values = new ArrayList<Integer>();
        for (int familyId : familyIds) {
            values.add(familyId);
        }
        ProjectFamilyRelatedArg arg = new ProjectFamilyRelatedArg();
        // TODO 根据业务场景，设置查询条件，示例
        ProjectFamilyRelatedCriteria criteria = arg.createCriteria();
        criteria.andProjectIdEqualTo(project);
        criteria.andFamilyIdIn(values);
        return projectFamilyRelatedDao.deleteByArg(arg);
    }

}
